package il.ac.tau.cg.ex3.parser.parametershander.lights;

import il.ac.tau.cg.ex3.entities.scene.Scene;
import il.ac.tau.cg.ex3.entities.scene.lights.DirectionalLight;
import il.ac.tau.cg.ex3.entities.styles.Color;
import il.ac.tau.cg.ex3.math.Vector;
import il.ac.tau.cg.ex3.parser.Parser;
import il.ac.tau.cg.ex3.parser.Parser.ParseException;
import il.ac.tau.cg.ex3.parser.parametershander.EntityParametersHandler;

public class DirectionalLightParametersHandler extends EntityParametersHandler
{
	private Color  color;
	private Vector direction;
	
	public DirectionalLightParametersHandler()
	{
		super();
		
		color      = new Color(1.0, 1.0, 1.0);
		direction  = null;
	}

	public boolean addParam(String key, String[] value) throws ParseException
	{	
		paramsCache.add(key);
		
		ELightParameters param =  ELightParameters.findParam(key);
		
		if (param == null)
		{
			return false;
		}
		
		Parser.pAssert((value.length == param.argsNum), param.name + " INVALID ARGS NUM");

		switch (param)
		{
			case COLOR:
			{	
				color = getColor(value);
			} break;

			case DIRECTION:
			{
				direction = getVector(value);
			} break;
		}
		
		return true;
	}

	protected void createEntity(Scene scene)
	{
		scene.addLight(new DirectionalLight(color, direction));
	}

	@Override
	protected boolean validateEntity() throws ParseException
	{
		for (ELightParameters param : ELightParameters.values())
		{
			if (param.isMandatory && !paramsCache.contains(param.name))
			{
				return false;
			}
		}
		
		return true;
	}
	
	private enum ELightParameters
	{
		/* LIGHTS ENTITIES*/
		COLOR("color", false, 3),
		
		/* LIGHT DIRECTED ENTITIES */
		DIRECTION("direction", true, 3);

		private final String  name;
		private final boolean isMandatory;
		private final int     argsNum;
		
		ELightParameters(String name, boolean isMandatory, int argsNum)
		{
			this.name = name;
			this.isMandatory = isMandatory;
			this.argsNum = argsNum;
		}
		
		public static ELightParameters findParam(String name)
		{
			for (ELightParameters param : ELightParameters.values())
			{
				if (param.name.equals(name))
				{
					return param;
				}
			}
			
			return null;
		}
	}
}
